package cn.edu.buaa.cnsatm.traffic_big_data_server.integrated.service;

import cn.edu.buaa.cnsatm.traffic_big_data_server.aviation.service.AirportService;
import cn.edu.buaa.cnsatm.traffic_big_data_server.highway.service.HighwayCongestionService;
import cn.edu.buaa.cnsatm.traffic_big_data_server.highway.service.HighwayTSService;
import cn.edu.buaa.cnsatm.traffic_big_data_server.integrated.dao.RoadStatusDAO;

import javax.json.Json;
import javax.xml.transform.Result;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.*;

import cn.edu.buaa.cnsatm.traffic_big_data_server.integrated.model.RoadStatusModel;
import cn.edu.buaa.cnsatm.traffic_big_data_server.util.JDBCUtil;
import cn.edu.buaa.cnsatm.traffic_big_data_server.util.PropertiesUtil;
import cn.edu.buaa.cnsatm.traffic_big_data_server.waterway.service.PortService;
import cn.edu.buaa.cnsatm.traffic_big_data_server.waterway.service.SeaStatusService;
import cn.edu.buaa.cnsatm.traffic_big_data_server.waterway.service.TyphoonService;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.activemq.artemis.api.core.Pair;

public class RoadStatusService {
    public static String getTopKRoadEvent(int k) throws SQLException {
        /**
         * @author LX
         * @description 获取铁水公空的事件：军事活动、事故、事件、地质灾害，一个大类型最多取k个
         * @date 2019-11-05
         */
        ResultSet resultSet = RoadStatusDAO.getTopKRoadEvent(k);
        List<RoadStatusModel> roadStatusList = new ArrayList<>();
        HashMap<String, Boolean> roadTypeMap = new HashMap<>();
        while(resultSet.next()){
            roadStatusList.add(new RoadStatusModel(resultSet));
            //System.out.println(roadStatusList);
            if (!roadTypeMap.containsKey(resultSet.getString("road_type"))){
                roadTypeMap.put(resultSet.getString("road_type"), true);
            }
        }
        resultSet.getStatement().close();
        resultSet.close();
        JsonArray ja = new JsonArray();
        for (Map.Entry<String, Boolean> entry: roadTypeMap.entrySet()) {
            String roadType = entry.getKey();
            //System.out.println(roadType);
            JsonObject jo = new JsonObject();
            jo.addProperty("traffic_type", roadType);
            JsonArray roadStatusJa = new JsonArray();
            for(RoadStatusModel roadStatus: roadStatusList){
                String curRoadType = roadStatus.getRoad_type();
                if(roadType.equals(curRoadType)){
                    JsonObject roadStatusJo = new JsonObject();
                    roadStatusJo.addProperty("event_type", roadStatus.getBlock_reason());
                    roadStatusJo.addProperty("event_title", roadStatus.getBlock_title());
                    roadStatusJo.addProperty("event_description", roadStatus.getBlock_description());
                    roadStatusJo.addProperty("update_time", roadStatus.getUpdate_time());
                    roadStatusJa.add(roadStatusJo);
                }
            }
            jo.add("event_list", roadStatusJa);
            ja.add(jo);
        }
        return ja.toString();
    }
    public static String getRoadStatusSQLString(String cookie) throws ParseException, SQLException, InterruptedException, IOException {
        String sqlString = new String("-- SQL语句：\n");
        //PropertiesUtil.setProperties();
        //sqlString = sqlString + HighwayTSService.getHighWayTSFromWebSaveToDatabase();
        //sqlString = sqlString + HighwayTSService.getHighwayAccidentFromWebSaveToDatabase();
        //sqlString = sqlString + HighwayTSService.getWeatherAlarmFromWebAndSaveToDatabase();
        //sqlString = sqlString + SeaStatusService.getAllSeaStatusSaveToDataBase();
        //sqlString = sqlString + SeaStatusService.getSeaBlockFromWebAndSaveToDatabase();
        //sqlString = sqlString + HighwayTSService.getHighwayBlockFromWebSaveToDatabase();
//        System.out.println(1);
//        sqlString = sqlString + HighwayCongestionService.getHighwayCongestionFromWebSaveToDatabase();
//        System.out.println(2);
        //sqlString = sqlString + AirportService.getAllAirportWeatherFromWebSaveToDatabase();
//        System.out.println(3);
        sqlString = sqlString + TyphoonService.getTyphoonFromWebAndSaveToDatabase();
        //sqlString = sqlString + PortService.getPortCongestionFromWebAndSaveToDatabase(cookie);
        return sqlString;
    }
    public static void main(String args[]) throws IOException, SQLException, ParseException, InterruptedException {
        JDBCUtil.startConnection();
        System.out.println(getRoadStatusSQLString("_elane_maptype=MT_SEA; Hm_lvt_adc1d4b64be85a31d37dd5e88526cc47=1571897877,1571918706,1573730663; tc_TC=; _elane_shipfilter_type=%u8D27%u8239%2C%u96C6%u88C5%u7BB1%u8239%2C%u6CB9%u8F6E%2C%u5F15%u822A%u8239%2C%u62D6%u8F6E%2C%u62D6%u5F15%2C%u6E14%u8239%2C%u6355%u635E%2C%u5BA2%u8239%2C%u641C%u6551%u8239%2C%u6E2F%u53E3%u4F9B%u5E94%u8239%2C%u88C5%u6709%u9632%u6C61%u88C5%u7F6E%u548C%u8BBE%u5907%u7684%u8239%u8236%2C%u6267%u6CD5%u8247%2C%u5907%u7528-%u7528%u4E8E%u5F53%u5730%u8239%u8236%u7684%u4EFB%u52A1%u5206%u914D%2C%u5907%u7528-%u7528%u4E8E%u5F53%u5730%u8239%u8236%u7684%u4EFB%u52A1%u5206%u914D%2C%u533B%u7597%u8239%2C%u7B26%u540818%u53F7%u51B3%u8BAE%28Mob-83%29%u7684%u8239%u8236%2C%u62D6%u5F15%u5E76%u4E14%u8239%u957F%3E200m%u6216%u8239%u5BBD%3E25m%2C%u758F%u6D5A%u6216%u6C34%u4E0B%u4F5C%u4E1A%2C%u6F5C%u6C34%u4F5C%u4E1A%2C%u53C2%u4E0E%u519B%u4E8B%u884C%u52A8%2C%u5E06%u8239%u822A%u884C%2C%u5A31%u4E50%u8239%2C%u5730%u6548%u5E94%u8239%2C%u9AD8%u901F%u8239%2C%u5176%u4ED6%u7C7B%u578B%u7684%u8239%u8236%2C%u5176%u4ED6; _elane_shipfilter_length=0%2C40%2C41%2C80%2C81%2C120%2C121%2C160%2C161%2C240%2C241%2C320%2C321%2C9999; _elane_shipfilter_sog=0%2C1; _elane_shipfilter_country=1; _filter_flag=-1; _elane_shipfilter_olength=0; tc_QX=; leadPageCookie=1; shipxy_v3_history_serch=s%u2606YANCHENGJI01805%u2606413900011%u260670%u2606MMSI%uFF1A413900011%7Cs%u2606JUFENG829%u2606413903389%u260670%u2606MMSI%uFF1A413903389%7Cp%u2606%u5317%u6D25%u2606213527%u260621.833334%u2606111.966667%7Cp%u2606Gangavaram%u2606218139%u260617.62645%u260683.2484%7Cs%u2606AO%20TOU%203243%u26063243%u2606%u2606MMSI%uFF1A3243%7Cs%u2606AO%20TOU%206789%2002%u2606678902%u2606%u2606MMSI%uFF1A678902%7Cs%u2606TRAILBLAZER%u2606366082000%u260630%u2606IMO%uFF1A7729069%7Cs%u2606SYMI%20I%u2606636016306%u2606100%u2606MMSI%uFF1A636016306%7Cs%u2606DONGPING120651%u260650087102%u2606%u2606MMSI%uFF1A50087102%7Cs%u2606YUEDONGGUANYU92020%u2606412468699%u260630%u2606MMSI%uFF1A412468699; .UserAuth2=9059D37AA149D2FAFC4CA9FF5071381A03B3E18230E95871D35106D820762CE480DE0CCE98FC7C73A166B9DBD583C3BF10DD1BA85B512F4E533DD813D7661F0374581A9008B899B24CA22409FEAE20317F13FFBC7529E4ABB4369E78D9E19921BF91C3475961561E373471F51ED88B935FE9953F; FD857C2AF68165D4=/vvpckUpWYbqsf/AR2UR989/U3+c1rQxrmBBjlg2xoaGuleYSZ0MY3AnhtgfgRtl9yVLHl6r4io=; SERVERID=ce54c768aca7be22386d8a7ce24ecdae|1573734191|1573730667; Hm_lpvt_adc1d4b64be85a31d37dd5e88526cc47=1573730663"));
    }
}
